﻿using System;
using System.ServiceModel;
using VideoStore.Business.Facade;
using VideoStore.Domain.Entities;
using VideoStore.Services.Dto;
using VideoStore.Services.Interface;
using VideoStore.Services.Translation;
using VideoStore.Services.Translation.Base;
using VideoStore.Services.Translation.Factory;

namespace VideoStore.Services
{
    public class OrderServiceQueued : IOrderServiceQueued
    {
        /// <summary>
        /// Updates the order status.
        /// </summary>
        /// <param name="modifiedOrderDto">The modified order dto.</param>
        [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
        public void UpdateOrderStatus(OrderDto modifiedOrderDto)
        {
            try
            {
                var facade = VideoStoreFacade.Instance;
                ITranslator translator = TranslatorFactory.GetTranslatorByType(typeof(OrderTranslator));
                Order orderEntity = facade.GetOrderById(modifiedOrderDto.Id);
                orderEntity.status = (int)modifiedOrderDto.OrderStatus;
                Console.WriteLine("Got request for updating Order Id {0} to status {1}", modifiedOrderDto.Id, modifiedOrderDto.OrderStatus);
                facade.UpdateOrderStatus(orderEntity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
